// Copyright 2022 NullPTR. All Rights Reserved.


#include "FRGBABitmap.h"

FRGBABitmap::FRGBABitmap() : ImageData(), Width(0), Height(0)
{
}

FRGBABitmap::FRGBABitmap(int w, int h)
{
	Create(w, h);
}

FRGBABitmap::FRGBABitmap(int w, int h, void* pDataBuf)
{
	check(pDataBuf);
	Width = w;
	Height = h;
	ImageData.Init(FColor::Transparent, Width * Height);
}

void FRGBABitmap::Create(int w, int h)
{
	Width = w;
	Height = h;
	if (Width * Height == 0)
	{
		ImageData.Reset();
	}
	else
	{
		ImageData.Init(FColor::Transparent, Width * Height);
	}
}

void FRGBABitmap::GetRGBA(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b, unsigned char& a) const
{
	size_t i = y * Width + x;
	
	r = ImageData[i].R;
	g = ImageData[i].G;
	b = ImageData[i].B;
	a = ImageData[i].A;
}

void FRGBABitmap::GetRGB(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b) const
{
	size_t i = y * Width + x;
	
	r = ImageData[i].R;
	g = ImageData[i].G;
	b = ImageData[i].B;
}

void FRGBABitmap::SetRGBA(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
	ImageData[y * Width + x] = FColor(r, g, b, a);
}

void FRGBABitmap::SetRGB(int x, int y, unsigned char r, unsigned char g, unsigned char b)
{
	ImageData[y * Width + x] = FColor(r, g, b, 1.0);
}

const TArray<FColor>& FRGBABitmap::GetData() const
{
	return ImageData;
}

UTexture2D* FRGBABitmap::ToUTexture2D()
{
	return nullptr;
}

FRGBABitmap::~FRGBABitmap()
{
	ImageData.Reset();
}
